/*
 * @Author: weidewei
 * @Date: 2025-05-19 09:09:42
 * @LastEditors: weidewei
 * @LastEditTime: 2025-05-19 09:24:12
 * @Description:
 * @FilePath: \v3\src\router\index.ts
 */
import { createRouter, createWebHashHistory, createWebHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";

const components = import.meta.glob("../views/**/index.vue");
const pages = import.meta.glob("../views/**/page.js", { eager: true, import: "default" });

const routes = Object.entries(pages).map(([path, meta]) => {
	// console.log(path);
	const componentPath = path.replace("/page.js", "/index.vue");
	const coms = components[componentPath];
	path = path.replace("../views", "").replace("/page.js", "") || "/";
	// console.log(routePath);
	const routeName = path.split("/").filter(Boolean).join("-");
	return {
		path,
		name: routeName,
		component: coms,
		meta
	};
});
console.log("routes", routes);
const router = createRouter({
	history: createWebHashHistory(),
	// history: createWebHistory()	,
	routes: [
		{
			path: "/",
			redirect: "/home" // 重定向到首页
		},
		{
			path: "/about",
			name: "about",
			component: () => import("../views/about/index.vue")
		},
		{
			path: "/about/detail/:id",
			name: "about-detail",
			component: () => import("../views/about/detail/index.vue")
		},
		// 使用parmas路由参数会有问题，所以要注释掉动态生成的路由
		...(routes as RouteRecordRaw[])
	]
});
console.log(router.getRoutes)

export default router;
